from fastapi import APIRouter, Request, status
from fastapi import HTTPException
from api.application.utils.logs import getLogger
import os
from api.application.utils.tools import generate_random_string
from api.application import settings
from api.application.utils import aliyuncloud
from api.application.utils.logs import getLogger

logger = getLogger(os.environ.get('APP_NAME'))

app = APIRouter()

aliyunclient = aliyuncloud.AliYunClound()

@app.get("/sms/{mobile}")
async def sms(request:Request, mobile: str):
    redis = await request.app.state.redis
    sms_code = generate_random_string(int(settings.SMS.get('length')))
    await redis.setex(f'SMS_{mobile}', settings.SMS.get('expire'), sms_code)
    print("sms_code:::", sms_code)
    # result = await aliyunclient.sms(
    #     sign_name=settings.ALIYUN['sms']['sign_name'],
    #     template_code=settings.ALIYUN['sms']['template_code'],
    #     mobile=mobile,
    #     template_param={'code': sms_code}
    # )
    # print(result)
    # if result.code != 'OK':
    #     logger.error(f"短信发送失败。。。{result.message}")
    #     raise HTTPException(status_code=status.HTTP_400_BAD_REQUEST, detail="短信发送失败")
    return {
        'code': 200,
        'err_msg': '短信已发送，请留意手机',
        'status': "Success"
    }
